﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Reflection;

namespace TfsTasksList
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {


        #region variables

        TeamFoundationServer tfs = null;
        VersionControlServer versionControl = null;
        WorkItemStore workItemStore = null;

        #endregion

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dtDateFrom.SelectedDate = DateTime.Now;
                btnConnect.IsEnabled = false;
                tfs = new TeamFoundationServer(txtServerUrl.Text);
                tfs.Authenticate();
                workItemStore = new WorkItemStore(txtServerUrl.Text);
                txtAuthUser.Text = tfs.AuthenticatedUserName;
                txtDisplayName.Text = tfs.AuthenticatedUserDisplayName;

                cmbProjects.ItemsSource = workItemStore.Projects;
                cmbProjects.IsEnabled = true;


                cmbFields.ItemsSource = workItemStore.FieldDefinitions;
                cmbFields.IsEnabled = true;


            }
            catch (Exception ex)
            {
                ManageException(ex);
                btnConnect.IsEnabled = true;
            }
        }

        private void ManageException(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }



        private void btnRetrieveTasks_Click(object sender, RoutedEventArgs e)
        {
            if (workItemStore == null)
                return;

            Project currentProject = cmbProjects.SelectedItem as Project;
            if (currentProject == null)
                return;


            WorkItemCollection Tasks = workItemStore.Query(
                " SELECT * " +
                " FROM WorkItems " +
                " WHERE [System.TeamProject] = '" + currentProject.Name +
                "' and [System.CreatedDate] >= '" +  dtDateFrom.SelectedDate.Value.ToString("yyyy-MM-dd") + "' ORDER BY [System.WorkItemType], [System.Id]");

            grdtasks.AutoGenerateColumns = false;
            grdtasks.ItemsSource = Tasks;
            
        }

        private void cmbFields_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            FieldDefinition fd = cmbFields.SelectedItem as FieldDefinition;
            if(fd== null)
                return;

            string msgValue = "";
            PropertyInfo[] ps = typeof(FieldDefinition).GetProperties();
            foreach(PropertyInfo pi in ps)
            {
                msgValue += pi.Name + " = " + Convert.ToString(pi.GetValue(fd,null)) + "\r";
                if (pi.Name == "ReferenceName")
                    txtelement.Text = Convert.ToString(pi.GetValue(fd, null));

            }

           
            MessageBox.Show(msgValue);
        }

        private void btnCustomQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (workItemStore == null)
                    return;

                Project currentProject = cmbProjects.SelectedItem as Project;
                if (currentProject == null)
                    return;

                WorkItemCollection Tasks = workItemStore.Query(txtQuery.Text);
                grdtasks.AutoGenerateColumns = true;
                grdtasks.ItemsSource = Tasks;
            }
            catch (Exception ex)
            {
                ManageException(ex);
            }
        }



    }
}
